<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>作家管理</title>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <style>
        /* 作家管理页面样式 */
        .writer-container {
            padding: 20px;
            max-width: 1200px;
            margin: 0 auto;
        }

        /* 操作栏 */
        .action-bar {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }

        /* 搜索框 */
        .search-box {
            display: flex;
            align-items: center;
        }

        .search-box input {
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            margin-right: 10px;
        }

        .search-box button {
            padding: 8px 16px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        .search-box button:hover {
            background-color: #0056b3;
        }

        /* 表格样式 */
        .writer-table {
            width: 100%;
            border-collapse: collapse;
            background-color: white;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }

        .writer-table th,
        .writer-table td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }

        .writer-table th {
            background-color: #f8f9fa;
            font-weight: 600;
        }

        .writer-table tr:hover {
            background-color: #f1f1f1;
        }

        /* 操作按钮 */
        .action-btn {
            padding: 5px 10px;
            margin: 0 5px;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }

        .ban-btn {
            background-color: #dc3545;
            color: white;
        }

        .unban-btn {
            background-color: #28a745;
            color: white;
        }

        .edit-btn {
            background-color: #ffc107;
            color: black;
        }

        /* 弹窗样式 */
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.5);
            z-index: 1000;
        }

        .modal-content {
            background-color: white;
            width: 80%;
            max-width: 800px;
            padding: 20px;
            border-radius: 8px;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            max-height: 80vh;
            overflow-y: auto;
        }

        .modal-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 1px solid #ddd;
            padding-bottom: 10px;
            margin-bottom: 20px;
        }

        .close-btn {
            font-size: 24px;
            cursor: pointer;
            color: #666;
        }

        .form-group {
            margin-bottom: 15px;
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: 500;
        }

        .form-group input,
        .form-group select {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }

        .form-actions {
            text-align: right;
            margin-top: 20px;
        }

        .form-actions button {
            margin-left: 10px;
        }

        /* 悬浮提示 */
        .name-hover {
            position: relative;
            cursor: pointer;
        }

        .name-hover:hover::after {
            content: "点击查看作品";
            position: absolute;
            top: -25px;
            left: 50%;
            transform: translateX(-50%);
            background-color: #333;
            color: #fff;
            padding: 5px 10px;
            border-radius: 4px;
            font-size: 12px;
            white-space: nowrap;
        }

        /* 作品表格样式 */
        .image-table {
            width: 120%;
            border-collapse: collapse;
            margin-top: 20px;
        }

        .image-table th,
        .image-table td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }

        .image-table th {
            background-color: #f8f9fa;
            font-weight: 600;
        }

        .image-table tr:hover {
            background-color: #f1f1f1;
        }
        .thumbnail-cell {
            width: 120px;
        }

        .thumbnail-img {
            
            height: 60px;
            object-fit: cover;
            border-radius: 4px;
        }

        /* 弹窗样式 */
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.5);
            z-index: 1000;
        }

        .modal-content {
            background-color: white;
            width: 80%;
            max-width: 800px;
            padding: 20px;
            border-radius: 8px;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            max-height: 80vh;
            overflow-y: auto;
        }

        .modal-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 1px solid #ddd;
            padding-bottom: 10px;
            margin-bottom: 20px;
        }

        .close-btn {
            font-size: 24px;
            cursor: pointer;
            color: #666;
        }

        .form-group {
            margin-bottom: 15px;
        }

        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: 500;
        }

        .form-group input,
        .form-group select {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }

        .form-actions {
            text-align: right;
            margin-top: 20px;
        }

        .form-actions button {
            margin-left: 10px;
        }
    </style>
</head>
<body>
    <div class="writer-container">
        <div class="action-bar">
            <h2>作家管理</h2>
            <div class="search-box">
                <input type="text" id="search-input" placeholder="输入作家名或ID">
                <button onclick="searchWriter()">搜索</button>
                <!-- 新增管理员按钮 -->
                <button class="action-btn" onclick="openAddModal()">新增管理员</button>
            </div>
            
        </div>

        <!-- 作家表格 -->
        <table class="writer-table">
            <thead>
                <tr>
                    <th>作家ID</th>
                    <th>作家名</th>
                    <th>电话号码</th>
                    <th>权限</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody id="writer-body">
                <!-- 数据将通过JavaScript动态生成 -->
            </tbody>
        </table>
    </div>

    <!-- 编辑作家弹窗 -->
    <div id="editModal" class="modal">
        <div class="modal-content">
            <div class="modal-header">
                <h3>编辑作家</h3>
                <span class="close-btn" onclick="closeEditModal()">&times;</span>
            </div>
            <form id="editForm" onsubmit="editWriter(event)">
                <div class="form-group">
                    <label for="editWriterName">作家名</label>
                    <input type="text" id="editWriterName" required>
                </div>
                <div class="form-group">
                    <label for="editPhoneNumber">电话号码</label>
                    <input type="text" id="editPhoneNumber" required>
                </div>
                <div class="form-group">
                    <label for="editRights">权限</label>
                    <select id="editRights" required>
                        <option value="super">超级管理员</option>
                        <option value="normal">普通管理员</option>
                    </select>
                </div>
                <div class="form-actions">
                    <button type="button" class="action-btn" onclick="closeEditModal()">取消</button>
                    <button type="submit" class="action-btn">保存</button>
                </div>
            </form>
        </div>
    </div>

    <!-- 作品展示弹窗 -->
    <div id="imageModal" class="modal">
        <div class="modal-content">
            <div class="modal-header">
                <h3>作品列表</h3>
                <span class="close-btn" onclick="closeImageModal()">&times;</span>
            </div>
            <table class="image-table">
                <thead>
                    <tr>
                        <th>图片ID</th>
                        <th>作品</th>
                        <th>标题</th>
                        <th>描述</th>
                        <th>上传时间</th>
                        <th>审核状态</th>
                        <th>VIP专属</th>
                        <th>标签</th>
                    </tr>
                </thead>
                <tbody id="image-body">
                    <!-- 数据将通过JavaScript动态生成 -->
                </tbody>
            </table>
        </div>
    </div>
    <!-- 新增管理员弹窗 -->
    <div id="addModal" class="modal">
        <div class="modal-content">
            <div class="modal-header">
                <h3>新增管理员</h3>
                <span class="close-btn" onclick="closeAddModal()">&times;</span>
            </div>
            <form id="addForm" onsubmit="addAdmin(event)">
                <div class="form-group">
                    <label for="addAdminName">管理员名</label>
                    <input type="text" id="addAdminName" required>
                </div>
                <div class="form-group">
                    <label for="addPassword">密码</label>
                    <input type="password" id="addPassword" required>
                </div>
                <div class="form-group">
                    <label for="addPhoneNumber">电话号码</label>
                    <input type="text" id="addPhoneNumber" required>
                </div>
                <div class="form-actions">
                    <button type="button" class="action-btn" onclick="closeAddModal()">取消</button>
                    <button type="submit" class="action-btn">保存</button>
                </div>
            </form>
        </div>
    </div>

    <script>
        let currentEditWriterId = null; // 当前编辑的作家ID

        document.addEventListener('DOMContentLoaded', () => {
            loadWriters();
        });

        // 加载作家列表
        function loadWriters() {
            axios({
                method: 'get',
                url: 'http://127.0.0.1:5000/admin/admins',
                headers: {
                    'Content-Type': 'application/json',
                }
            })
            .then(function (response) {
                if (response.data) {
                    const admins = response.data.admins;
                    renderWriterTable(admins);
                    console.log(admins);
                } else {
                    alert('获取作家列表失败：' + response.data.message);
                }
            })
            .catch(function (error) {
                console.error('加载作家列表失败：', error);
                if (error.response) {
                    alert('加载作家列表失败：' + (error.response.data.error || '服务器错误'));
                } else {
                    alert('加载作家列表失败：网络错误，请检查连接');
                }
            });
        }

        // 删除作家
        function deleteWriter(adminsId) {
            axios({
                method: 'delete',
                url: `http://127.0.0.1:5000/admin/admins/${adminsId}`,
                headers: {
                    'Content-Type': 'application/json',
                }
            })
            .then(function (response) {
                console.log(response.data);
                if (response.data) {
                    alert('作家删除成功！');
                    loadWriters(); // 重新加载作家列表
                } else {
                    alert('作家删除失败：' + response.data.message);
                }
            })
            .catch(function (error) {
                console.log('删除作家失败：', error.message);
                if (error.response) {
                    alert('删除作家失败：' +'服务器错误');
                    console.log(error.response.data.error);
                } else {
                    alert('删除作家失败：网络错误，请检查连接');
                }
            });
        }

        // 渲染作家表格
        function renderWriterTable(data) {
            const tbody = document.getElementById('writer-body');
            tbody.innerHTML = data.map(writer => {
                return `
                    <tr>
                        <td>${writer.admin_id}</td>
                        <td class="name-hover" onclick="loadImages(${writer.admin_id})">${writer.admin_name}</td>
                        <td>${writer.phone}</td>
                        <td>${writer.rights === 'super' ? '超级管理员' : '普通管理员'}</td>
                        <td>
                            <button class="action-btn edit-btn" onclick="openEditModal(${writer.admin_id})">编辑</button>
                            <button class="action-btn delete-btn" onclick="deleteWriter(${writer.admin_id})">删除</button>
                        </td>
                    </tr>
                `;
            }).join('');
        }

        // 打开编辑弹窗
        function openEditModal(adminsId) {
            currentEditWriterId = adminsId;
            axios({
                method: 'get',
                url: `http://127.0.0.1:5000/admin/admins/${adminsId}`,
                headers: {
                    'Content-Type': 'application/json',
                }
            })
            .then(function (response) {
                console.log(response.data);
                const admins = response.data;
                if (admins) {
                    document.getElementById('editWriterName').value = admins.admin_name;
                    document.getElementById('editPhoneNumber').value = admins.phone;
                    document.getElementById('editRights').value = admins.rights;
                    document.getElementById('editModal').style.display = 'block';
                } else {
                    console.log('作家不存在');
                }
            })
            .catch(function (error) {
                console.log('获取作家信息失败：', error);
                if (error.response) {
                    alert('获取作家信息失败：' + (error.response.data.error || '服务器错误'));
                } else {
                    alert('获取作家信息失败：网络错误，请检查连接');
                }
            });
        }

        // 关闭编辑弹窗
        function closeEditModal() {
            document.getElementById('editModal').style.display = 'none';
            currentEditWriterId = null;
        }

        // 编辑作家
        function editWriter(event) {
            event.preventDefault();
            const writerName = document.getElementById('editWriterName').value;
            const phoneNumber = document.getElementById('editPhoneNumber').value;
            const rights = document.getElementById('editRights').value;

            axios({
                method: 'put',
                url: `http://127.0.0.1:5000/admin/admins/${currentEditWriterId}`,
                headers: {
                    'Content-Type': 'application/json',
                },
                data: {
                    admin_name: writerName,
                    phone: phoneNumber,
                    rights: rights
                }
            })
            .then(function (response) {
                if (response.data) {
                    alert('作家信息更新成功！');
                    loadWriters(); // 重新加载作家列表
                    closeEditModal();
                } else {
                    alert('作家信息更新失败：' + response.data.message);
                }
            })
            .catch(function (error) {
                console.log('更新作家信息失败：', error);
                if (error.response) {
                    alert('更新作家信息失败：' + (error.response.data.error || '服务器错误'));
                } else {
                    alert('更新作家信息失败：网络错误，请检查连接');
                }
            });
        }

        // 搜索作家
        function searchWriter() {
            const keyword = document.getElementById('search-input').value.toLowerCase();
            axios({
                method: 'get',
                url: 'http://127.0.0.1:5000/admin/admins',
                headers: {
                    'Content-Type': 'application/json',
                }
            })
            .then(function (response) {
                const admins = response.data.admins;
                const filteredWriters = admins.filter(admins =>
                    admins.admin_name.toLowerCase().includes(keyword) || admins.admin_id.toString().includes(keyword)
                );
                renderWriterTable(filteredWriters);
            })
            .catch(function (error) {
                console.error('搜索作家失败：', error);
                if (error.response) {
                    alert('搜索作家失败：' + (error.response.data.error || '服务器错误'));
                } else {
                    alert('搜索作家失败：网络错误，请检查连接');
                }
            });
        }

        // 加载管理员发布的作品
        function loadImages(adminId) {
            axios({
                method: 'get',
                url: `http://127.0.0.1:5000/admin/images/${adminId}`,
                headers: {
                    'Content-Type': 'application/json',
                }
            })
            .then(function (response) {
                const images = response.data.images;
                renderImageTable(images);
                document.getElementById('imageModal').style.display = 'block';
            })
            .catch(function (error) {
                console.error('加载作品失败：', error);
                if (error.response) {
                    alert('加载作品失败：' + (error.response.data.error || '服务器错误'));
                } else {
                    alert('加载作品失败：网络错误，请检查连接');
                }
            });
        }

        // 渲染作品表格
        function renderImageTable(data) {
            
            const tbody = document.getElementById('image-body');
            tbody.innerHTML = data.map(image => {
                // 格式化时间，只显示年月日
                const uploadDate = new Date(image.upload_time);
                const formattedDate = uploadDate.toISOString().split('T')[0]; // 获取 YYYY-MM-DD 格式
                return `
                    <tr>
                        
                        <td>${image.image_id}</td>
                        <td class="thumbnail-cell">
                        <img src="${image.oss_url}" alt="${image.title}" class="thumbnail-img">
                        </td>
                        <td>${image.title}</td>
                        <td>${image.description}</td>
                        <td>${formattedDate}</td>
                        <td>${image.audit_status}</td>
                        <td>${image.vip_exclusive}</td>
                        <td>${image.tags.join(', ')}</td>
                    </tr>
                `;
            }).join('');
        }

        // 关闭作品展示弹窗
        function closeImageModal() {
            document.getElementById('imageModal').style.display = 'none';
        }

        // 打开新增管理员弹窗
        function openAddModal() {
            document.getElementById('addModal').style.display = 'block';
        }

        // 关闭新增管理员弹窗
        function closeAddModal() {
            document.getElementById('addModal').style.display = 'none';
        }

        // 新增管理员
        function addAdmin(event) {
            event.preventDefault();
            const adminName = document.getElementById('addAdminName').value;
            const password = document.getElementById('addPassword').value;
            const phoneNumber = document.getElementById('addPhoneNumber').value;

            axios({
                method: 'post',
                url: 'http://127.0.0.1:5000/admin/admins',
                headers: {
                    'Content-Type': 'application/json',
                },
                data: {
                    admin_name: adminName,
                    password: password,
                    phone: phoneNumber
                }
            })
            .then(function (response) {
                if (response.data && response.data.admin_id) {
                    alert('管理员新增成功！');
                    loadWriters(); // 重新加载管理员列表
                    closeAddModal();
                } else {
                    alert('管理员新增失败：' + response.data.message);
                }
            })
            .catch(function (error) {
                console.error('新增管理员失败：', error);
                if (error.response) {
                    alert('新增管理员失败：' + (error.response.data.error || '服务器错误'));
                } else {
                    alert('新增管理员失败：网络错误，请检查连接');
                }
            });
        }
    </script>
</body>
</html>